package com.aic.aicdetactor.util;

import java.util.Arrays;

import com.aic.xj.app.util.StringUtil;

public class ArrayUtil {
    public static <T> T[] reverse(T[] objs) {
        for (int i = 0; i < objs.length / 2; i++) {
            T temp = objs[i];
            objs[i] = objs[objs.length - i - 1];
            objs[objs.length - i - 1] = temp;
        }
        return objs;
    }
    
    public static byte[] reverse(byte[] objs) {
        for (int i = 0; i < objs.length / 2; i++) {
            byte temp = objs[i];
            objs[i] = objs[objs.length - i - 1];
            objs[objs.length - i - 1] = temp;
        }
        return objs;
    }
    
    public static byte[] subArray(byte[] bytes,int position,int length)
    {
        return Arrays.copyOfRange(bytes, position, position + length);
    }
    
    
  //浮点到字节转换  
  	 public static byte[] doubleToBytes(double d)  
  	 {  
  	     byte writeBuffer[]= new byte[8]; 
  	     byte [] b = new byte[8]; 
  	      long v = Double.doubleToLongBits(d);  
  	         b[0] = (byte) (v & 0x000000000000FFL); 
  	         b[1] = (byte) ((v & 0x0000000000FF00L) >> 8); 
  	         b[2] = (byte) ((v & 0x0000000000FF0000L) >> 16); 
  	         b[3] = (byte) ((v & 0x00000000FF000000L) >> 24); 
  	         b[4] = (byte) ((v & 0x000000FF00000000L) >> 32); 
  	         b[5] = (byte) ((v & 0x0000FF0000000000L) >> 40); 
  	         b[6] = (byte) ((v & 0x00FF000000000000L) >> 48); 
  	         b[7] = (byte) ((v & 0xFF00000000000000L) >> 56);
  	         return b;  
  	   
  	 }
  	 //字节转换为16进制字符串
  	 public static String bytes2hex03(byte[] bytes)  
  	    {  
  	        final String HEX = "0123456789ABCDEF";  
  	        StringBuilder sb = new StringBuilder(bytes.length * 2);  
  	        for (byte b : bytes)  
  	        {  
  	            // 取出这个字节的高4位，然后与0x0f与运算，得到一个0-15之间的数据，通过HEX.charAt(0-15)即为16进制数  
  	            sb.append(HEX.charAt((b >> 4) & 0x0f));  
  	            // 取出这个字节的低位，与0x0f与运算，得到一个0-15之间的数据，通过HEX.charAt(0-15)即为16进制数  
  	            sb.append(HEX.charAt(b & 0x0f));  
  	        }  
  	  
  	        return sb.toString();  
  	    }  
  	 
  	 //字节数组转换为double
  	 public static double bytes2Double(byte[] arr) {  
  	        long value = 0;  
  	        for (int i = 0; i < 8; i++) {  
  	            value |= ((long) (arr[i] & 0xff)) << (8 * i);  
  	        }  
  	        return Double.longBitsToDouble(value);  
  	    } 
  	 
  	 /**
  	  * 16进制的字符串表示转成字节数组
  	  *
  	  * @param hexString
  	  *            16进制格式的字符串
  	  * @return 转换后的字节数组
  	  **/
  	 public static byte[] toByteArray(String hexString) {
  		  if (StringUtil.isNullOrWhiteSpace(hexString))
  		   throw new IllegalArgumentException("this hexString must not be empty");
  		 
  		  hexString = hexString.toLowerCase();
  		  final byte[] byteArray = new byte[hexString.length() / 2];
  		  int k = 0;
  		  for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
  		   byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
  		   byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
  		   byteArray[i] = (byte) (high << 4 | low);
  		   k += 2;
  		  }
  		  return byteArray;
  		 }
}
